package com.atguigu.gmall.list.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.dao.GoodsDao;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.*;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuAttrValue;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Mamba
 * @version 1.0
 * @Description
 * @date 2021/1/1 14:21
 */
@Service
public class ListServiceImpl implements ListService {
    //连接ES索引库   功能主要是查询  查询速度性能非常好
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private RedisTemplate redisTemplate;//Redis Jedis  客户端 困难   MybatisPlus
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private GoodsDao goodsDao;//ES

    //上架    商品 sku  到ES索引库
    @Override
    public void onSale(Long skuId) {
        Goods goods = new Goods();
        //补全所有的数据
        //查询sku_info数据
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        //ID
        goods.setId(skuInfo.getId());
        //图片
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        //标题
        goods.setTitle(skuInfo.getSkuName());
        //价格
        goods.setPrice(skuInfo.getPrice().doubleValue());
        //////////////////////////////////////////////////////////////
        //时间
        goods.setCreateTime(new Date());
        /////////////////////////////////////////////////////////////
        //查询 base_trademark
        BaseTrademark baseTrademark = productFeignClient.getBaseTrademark(skuInfo.getTmId());
        //品牌ID
        goods.setTmId(baseTrademark.getId());
        //品牌名称
        goods.setTmName(baseTrademark.getTmName());
        //品牌的Logo
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());
        ///////////////////////////////////////////////////////////////
        //查询一二三级分类 视图base_category_view
        BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
        goods.setCategory1Id(baseCategoryView.getCategory1Id());
        goods.setCategory1Name(baseCategoryView.getCategory1Name());
        goods.setCategory2Id(baseCategoryView.getCategory2Id());
        goods.setCategory2Name(baseCategoryView.getCategory2Name());
        goods.setCategory3Id(baseCategoryView.getCategory3Id());
        goods.setCategory3Name(baseCategoryView.getCategory3Name());
        //查询平台属性表  （3张表）
        List<SkuAttrValue> baseAttrInfoAndBaseAttrValue = productFeignClient.getBaseAttrInfoAndBaseAttrValue(skuId);
        List<SearchAttr> attrs = baseAttrInfoAndBaseAttrValue.stream().map((skuAttrValue) -> {
            SearchAttr searchAttr = new SearchAttr();
            //平台属性ID
            searchAttr.setAttrId(skuAttrValue.getBaseAttrInfo().getId());
            //平台属性名称
            searchAttr.setAttrName(skuAttrValue.getBaseAttrInfo().getAttrName());
            //平台属性值名称
            searchAttr.setAttrValue(skuAttrValue.getBaseAttrValue().getValueName());
            return searchAttr;
        }).collect(Collectors.toList());
        goods.setAttrs(attrs);
        //添加、修改根据ID 存在了就修改，不存在就是添加
        goodsDao.save(goods);
    }


    //下架
    @Override
    public void cancelSale(Long skuId) {
        //删除
        goodsDao.deleteById(skuId);
    }

    //更新热度
    @Override
    public void hotScore(Long skuId) {

        //1.更新Redis缓存（五大数据类型）string,hash,list,set,zset
        String name = "hotScFore";
        //zset 原子性操作 不需要再上锁
        Double score = redisTemplate.opsForZSet().incrementScore(name, skuId, 1);
        //2.达到一定数量 才更新ES索引库
        if (score % 10 == 0) {//0到10 ，11到20...
            Optional<Goods> byId = goodsDao.findById(skuId);
            Goods goods = byId.get();
            goods.setHotScore(Math.round(score));
            goodsDao.save(goods);
        }
    }
    //开始搜索  SearchParam ： 带着所有条件形参
    //       SearchResponseVo ：将来要返回的返回值 对象
    @Override
    public SearchResponseVo list(SearchParam searchParam) {
        //1.构建条件对象
        SearchRequest searchRequest =  buildSearchRequest(searchParam);
        //2.执行搜索（ES)
        SearchResponse searchResponse =null;
        try {
            searchResponse = restHighLevelClient.
                    search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3.解析结果
        SearchResponseVo vo = parseSearchResponse(searchResponse);
        //当前页
        vo.setPageNo(searchParam.getPageNo());
        //每页数
        vo.setPageSize(searchParam.getPageSize());
        //计算总页数=（总条数+每页数-1）/每页数
        long totalPages = (vo.getTotal()+vo.getPageSize()-1)/vo.getPageSize();
        vo.setTotalPages(totalPages);
        return vo;
    }

    //解析结果
    private SearchResponseVo parseSearchResponse(SearchResponse searchResponse) {
        SearchResponseVo vo = new SearchResponseVo();
        //1.总条数
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("总条数："+totalHits);
        vo.setTotal(totalHits);
        //2.商品数据
        SearchHit[] hits1 = hits.getHits();
        List<Goods> goodsList = Arrays.stream(hits1).map((hit) -> {
            String sourceAsString = hit.getSourceAsString();
            Goods goods = JSONObject.parseObject(sourceAsString, Goods.class);
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if (null != highlightFields) {
                HighlightField title = highlightFields.get("title");
                if (title != null) {
                    String string = title.fragments()[0].toString();
                    System.out.println(string);
                    goods.setTitle(string);
                }
            }
            return goods;
        }).collect(Collectors.toList());
        vo.setGoodsList(goodsList);
        //解析品牌集合
        Map<String, Aggregation> stringAggregationMap =
                searchResponse.getAggregations().asMap();
        ParsedLongTerms tmIdAgg = (ParsedLongTerms)stringAggregationMap.get("tmIdAgg");
        //1 2 4
        List<SearchResponseTmVo> tmVoList = tmIdAgg.getBuckets().stream().map(bucket -> {
            SearchResponseTmVo tmVo = new SearchResponseTmVo();
            //品牌ID
            tmVo.setTmId(Long.parseLong(bucket.getKeyAsString()));
            //品牌名称
            ParsedStringTerms tmNameAgg = ((Terms.Bucket) bucket).getAggregations().get("tmNameAgg");
            tmVo.setTmName(tmNameAgg.getBuckets().get(0).getKeyAsString());
            //品牌Logo
            ParsedStringTerms tmLogoUrlAgg = ((Terms.Bucket) bucket).getAggregations().get("tmLogoUrlAgg");
            tmVo.setTmLogoUrl(tmLogoUrlAgg.getBuckets().get(0).getKeyAsString());
            return tmVo;
        }).collect(Collectors.toList());
        vo.setTrademarkList(tmVoList);
        //解析平台属性
        Nested attrsAgg = (Nested) stringAggregationMap.get("attrsAgg");
        Aggregations aggregations = attrsAgg.getAggregations();
        Terms attrIdAgg = aggregations.get("attrIdAgg");
        List<SearchResponseAttrVo> responseAttrVoList = attrIdAgg.getBuckets().stream().map(bucket->{
            SearchResponseAttrVo attrVo = new SearchResponseAttrVo();
            //一个平台属性Id
            String attrId = ((Terms.Bucket) bucket).getKeyAsString();
            attrVo.setAttrId(Long.parseLong(attrId));
            //一个平台属性名称
            Terms attrNameAgg = ((Terms.Bucket) bucket).getAggregations().get("attrNameAgg");
            attrVo.setAttrName(attrNameAgg.getBuckets().get(0).getKeyAsString());
            //多个平台属性值
            Terms attrValueAgg = ((Terms.Bucket) bucket).getAggregations().get("attrValueAgg");
            attrVo.setAttrValueList(attrValueAgg.getBuckets().stream()
            .map(Terms.Bucket::getKeyAsString).collect(Collectors.toList()));
            return attrVo;
        }).collect(Collectors.toList());
        vo.setAttrsList(responseAttrVoList);
        return vo;
    }

    //构建搜索请求对象
    private SearchRequest buildSearchRequest(SearchParam searchParam) {
        //请求对象
        SearchRequest searchRequest = new SearchRequest();//所有 索引库名称 执行搜索的时候 请求对象
        //构建搜索条件对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//排序 分页 聚合

        //构建组合对象（组合多个条件）
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();//关键词及过滤条件

        //开始向条件对象设置条件
        //searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //1.关键词  select * from 表 where title like %苹果% OR title like %手机%
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)){
            //must : 必须   where title like %苹果% and title like %手机%
            //mustNot : 不必须   where title like %苹果% not title like %手机%
            //should : 应该   where title like %苹果% OR title like %手机%
            boolQueryBuilder.must(
                    QueryBuilders.matchQuery("title",keyword).operator(Operator.AND));
        }else {
            //查询所有,非常耗费性能，轻易不要使用
            boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        }
        //2.过滤条件
            //品牌:      品牌ID:品牌的名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            String[] t = StringUtils.split(trademark, ":");
            boolQueryBuilder.filter(QueryBuilders.termQuery("tmId",t[0]));
        }


        //3.一二三级分类（三级分类）
        Long category1Id = searchParam.getCategory1Id();
        if (null!=category1Id){
            boolQueryBuilder.filter(QueryBuilders.termQuery("category1Id",category1Id));
        }
        Long category2Id = searchParam.getCategory2Id();
        if (null!=category2Id){
            boolQueryBuilder.filter(QueryBuilders.termQuery("category2Id",category2Id));
        }
        Long category3Id = searchParam.getCategory3Id();
        if (null!=category3Id){
            boolQueryBuilder.filter(QueryBuilders.termQuery("category3Id",category3Id));
        }
    //平台属性
        String[] props = searchParam.getProps();
        if (null!=props&&props.length>0){
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();//[ ]
            for (String prop : props) {
                //子组合对象
                BoolQueryBuilder subBoolQuery = QueryBuilders.boolQuery();//{ }
                //prop : 平台属性ID:平台属性值名称:平台属性名称
                String[] p = prop.split(":");
                subBoolQuery.must(QueryBuilders.termQuery("attrs.attrId",p[0]));
                subBoolQuery.must(QueryBuilders.termQuery("attrs.attrValue",p[1]));
                //嵌套关系
                boolQuery.must(QueryBuilders.nestedQuery("attrs",subBoolQuery,
                        ScoreMode.None));
            }
            //平台属性
            boolQueryBuilder.filter(boolQuery);
        }
        //开始向条件对象设置组合对象
        searchSourceBuilder.query(boolQueryBuilder);
        //4.排序
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            if (null!= o &&o.length==2){
                String s="";
                switch (o[0]){//很多
                    case "1":s = "hotScore";break;
                    case "2":s = "price";break;
                }
                searchSourceBuilder.sort(s,
                        o[1].equalsIgnoreCase("acs")? SortOrder.ASC:SortOrder.DESC);
            }
        }else {
            //排序 默认综合排序 按照额度排序（公平）
            searchSourceBuilder.sort("hotScore",SortOrder.DESC);
        }
        //5.分页   limit 开始行,每页数
        Integer pageNo = searchParam.getPageNo();
        Integer pageSize = searchParam.getPageSize();
        searchSourceBuilder.from((pageNo-1)*pageSize);
        searchSourceBuilder.size(pageSize);

        //6.高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title")
                .preTags("<font color='red'>").postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);
        //7：根据关键词 查询 品牌集合  8个数据  ： 华为2  小米1  苹果5
        // 华为 小米  苹果    group by  ==  ES 索引库 聚合 select max(id) as idddd from
        // 品牌ID 品牌名称 品牌Logo
        searchSourceBuilder.aggregation(
                AggregationBuilders.terms("tmIdAgg").field("tmId")
                .subAggregation(AggregationBuilders.terms("tmNameAgg").field("tmName"))
                .subAggregation(AggregationBuilders.terms("tmLogoUrlAgg").field("tmLogoUrl")));
        // 8:平台属性聚合
            searchSourceBuilder.aggregation(
                    AggregationBuilders.nested("attrsAgg","attrs")
                    .subAggregation(AggregationBuilders.terms("attrIdAgg").field("attrs.attrId")
                    .subAggregation(AggregationBuilders.terms("attrNameAgg").field("attrs.attrName"))
                    .subAggregation(AggregationBuilders.terms("attrValueAgg").field("attrs.attrValue")))
            );






    System.out.println(searchSourceBuilder.toString());//DSL

        //将构建搜索条件对象设置给请求对象
        searchRequest.source(searchSourceBuilder);
        //设置索引库
        searchRequest.indices("goods");
        //设置类型  6.8.1   type 还在  7.0以上 type 消失了
        //searchRequest.types("info");
        return searchRequest;
    }


}